package com.paranoidvulture.javaEngine.Utilities;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

/*This is the text box.
 * It has multiple values that must be used.
 * 1) It must have a position.
 * 2) It must have a width set, otherwise it will default to 100px
 * 3) To use it, you must set it active using setActive(true)
 * 4) You must run the update(long timePassed) method in the views update method
 * 5) You must use the draw(Graphics2D g) method in the views draw method
 * ---------------------------------------------------------------------------
 * In order to set the text of the textBox, you use the setText(String text) method
 * To get whatever the textBox it holding (text) use getText() method, returns string
*/
public class TextBox extends ControlBase {
	
	
	public TextBox() {
		this.setImage("TextBox.png");
	}
	
	//variables
	private float txtPosX;
	private float txtPosY;
	private String inputString;
	private boolean isSelected;
	private Rectangle2D rec;
	private Color bgColor, fgColor;
	private StringBuilder tempText = new StringBuilder();
	
	//font stuff
	private int fontSize = 10;
	private String fontType = "Serif";

	
	public float getTxtPosX(){
		return this.txtPosX;
	}
	
	public float getTxtPosY(){
		return this.txtPosY;
	}
	
	public void setTxtPosX(float x){
		this.txtPosX = x;
	}
	
	public void setTxtPosY(float y){
		this.txtPosY = y;
	}
	
	public Rectangle2D getRectangle() {
		return rec;
	}
	
	public void setActualTxtPos(){
		float x = this.getPosX();
		float y = this.getPosY();
		this.setTxtPosX(x + 5);
		this.setTxtPosY(y + ((float)this.getHeight() *.75f));
	}
	
	public void setText(String text){
		this.inputString = text;
	}
	
	public String getText(){
		return this.inputString;
	}
	
	public void init(){
		this.setWidth(200);
		this.setHeight(32);		
		this.isSelected = false;
		this.bgColor = Color.red;
	}	
	
	@Override
	public void update(long timePassed){
		if (this.getActive()){
			super.update(timePassed);
			this.setActualTxtPos();	
			rec = new Rectangle2D.Double((int)this.getPosX(), 
					(int)this.getPosY(), this.getWidth(), this.getHeight());
		}
	}
	
	
	//just make the damn text box out of rectangles and lines.
	public void draw(Graphics2D g){
		if (this.getActive()){
			//Use Fill to have a nice backdrop.
			g.setColor(bgColor);
			g.fill(rec);
			g.setColor(fgColor);
			g.draw(rec);
			g.setColor(Color.black);
			Font font = new Font(this.fontType, Font.PLAIN, this.fontSize);
			g.setFont(font);
			g.drawString(this.getText(), (int)this.getTxtPosX(), (int)this.getTxtPosY());
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (rec.contains(e.getX(), e.getY())) {
			bgColor = Color.orange;
			isSelected = true;
		} else { 
			isSelected = false; 
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		if(!isSelected) {
			if (rec.contains(e.getX(), e.getY())) {
				fgColor = Color.blue;
			} else {
				fgColor = Color.red;
				bgColor = Color.red;
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(isSelected) {
			if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
				if (this.getText() != "") {
					tempText.deleteCharAt(tempText.length() - 1);
				}
			} else if (e.getKeyCode() == KeyEvent.VK_SHIFT){
				//DO_NOTHING:
			}else {
				tempText.append(e.getKeyChar());
			}
			
			this.setText(tempText.toString());
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

	@Override
	public void keyTyped(KeyEvent e) {

	}
}

